﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.WebChuChuangManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.WebChuChuangManager
{

    public class WebChuChuangManagerController : BaseController
    {
        private readonly WebChuChuangTask _webchuchuangTask = new WebChuChuangTask();

        public ActionResult List(int? tid, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _webchuchuangTask.GetPagedList(tid,pageIndex, pageSize);


            ViewData["typelist"] = ChuChuangEnum.今天特价.ToSelectListForAll();
            var model = new PagedList<WebChuChuang>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {
            ViewData["typelist"] = ChuChuangEnum.今天特价.ToSelectListForAll();

            var model = new SaveWebChuChuang();

            if (id != null)
            {
                var item = _webchuchuangTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<WebChuChuang, SaveWebChuChuang>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveWebChuChuang savemodel)
        {
            ViewData["typelist"] = ChuChuangEnum.今天特价.ToSelectListForAll();
            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new WebChuChuang
                                    {
                                        ProductID = savemodel.ProductID,
                                        WebADType = savemodel.WebADType,
                                        Sort = savemodel.Sort,
                                    };
                    _webchuchuangTask.Add(model);
                }
                else
                {
                    var model = _webchuchuangTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.ProductID = savemodel.ProductID;
                    model.WebADType = savemodel.WebADType;
                    model.Sort = savemodel.Sort;


                    _webchuchuangTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _webchuchuangTask.Delete(id);
        }

        #endregion
    }
}